package cn.tedu.store.service.impl;


import cn.tedu.store.entity.User;
import cn.tedu.store.mapper.UserMapper;
import cn.tedu.store.service.IUserService;
import cn.tedu.store.service.ex.*;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.UUID;
@Service
public class UserServiceImpl implements IUserService {
	@Autowired
	private UserMapper userMapper;
	@Override
	public void reg(User user) throws UsernameDuplicateException, InsertException {
        String username=user.getUsername();
        User result=userMapper.findByUsername(username);
        if (result!=null) {
        	throw new UsernameDuplicateException("用户名"+username+"已存在");
        }
        user.setIs_delete(0);
        Date now=new Date();
        user.setCreated_time(now);
        user.setCreated_user(username);
        user.setModified_user(username);
        user.setModified_time(now);
        String salt=UUID.randomUUID().toString().toUpperCase();
        String passwotd_old=user.getPassword();
        String md5password=getMd5Password(user.getPassword(),salt);
       // System.err.println(md5password);
        user.setPassword(md5password);
        user.setSalt(salt);
        Integer rows=userMapper.insert(user);
        //System.err.println("password:"+md5password+"/salt:"+salt+"/passwotd_old:"+passwotd_old);
        if (rows !=1) {
        	throw new InsertException("插入数据失败");
        }
	}

	@Override
	public User login(String username, String password) throws UserNotFoundException, PasswordNotMatchException {
		User result=userMapper.findByUsername(username);
		if (result==null) {
			throw new UserNotFoundException("登录失败，用户不存在");
		}
		if (result.getIs_delete()==1) {
			throw new UserNotFoundException("登录失败，用户已作废");
		}
		String salt=result.getSalt();
		String md5password=getMd5Password(password,salt);
		if (!result.getPassword().equals(md5password)) {
			throw new PasswordNotMatchException("登录失败，密码错误");
		}
		result.setPassword(null);
		result.setSalt(null);
		result.setIs_delete(0);
		return result;
	}
	/**
	 * 修改密码
	 */
	@Override
	public void changPassword(Integer uid, String username, String oldPassword, String newPassword)
			throws UserNotFoundException, PasswordNotMatchException, UpdateException {
		//根据参数uid查询用户数据
		User result=userMapper.findByUid(uid);
		//判断查询结果是否为null
		//抛出UserNotFoundException异常
		if (result==null) {
			throw new UserNotFoundException("用户不存在");
		}
		//判断is_delete是否为1
		//抛出UserNotFoundException异常
		if (result.getIs_delete()==1) {
			throw new UserNotFoundException("用户已作废");
		}
		//从查询结果中获取盐值
		String salt=result.getSalt();
		//根据参数oldPassword和盐值一起加密，得到加密后的密码
		String oldMd5Password=getMd5Password(oldPassword,salt);
		//判断查询结果中的密码和加密后的密码是否一致
		if(!result.getPassword().equals(oldMd5Password)) {
			//抛出PasswordNotMatchException异常
			throw new PasswordNotMatchException("修改失败，原密码错误");
		}

		//根据参数newPassword和盐值进行加密得到加密后的密码
		String newMd5Password=getMd5Password(newPassword,salt);
		//创建当前时间对象
		Date now=new Date();
		//执行更新密码并获得影像的行数
		Integer rows=userMapper.updatePassword(uid, newMd5Password, username, now);
		//判断受影响的行数是否不为1
		if(rows!=1) {
		//抛出UpdateException异常
			throw new UpdateException("修改密码未知异常");
		}
	}

	@Override
	public void changeAvatar(Integer uid, String username, String avatar) throws UserNotFoundException, PasswordNotMatchException, UpdateException{
		        //根据参数uid查询用户数据
				User result=userMapper.findByUid(uid);
				//判断查询结果是否为null
				//抛出UserNotFoundException异常
				if (result==null) {
					throw new UserNotFoundException("用户不存在");
				}
				//判断is_delete是否为1
				//抛出UserNotFoundException异常
				if (result.getIs_delete()==1) {
					throw new UserNotFoundException("用户已作废");
				}

				//创建当前时间对象
				Date now=new Date();
				//执行更新头像并获得影像的行数
				Integer rows=userMapper.updateAvatar(uid, avatar, username, now);
				//判断受影响的行数是否不为1
				if(rows!=1) {
				//抛出UpdateException异常
					throw new UpdateException("修改头像未知异常");
				}

	}





	@Override
	public User getByUid(Integer uid) {
		//根据uid查询数据
        User result=userMapper.findByUid(uid);
		//查到数据后，将password,salt, is_delete设置为null
        if(result!=null) {
        	result.setPassword(null);
        	result.setSalt(null);
        	result.setIs_delete(0);
        }
		//将查询结果返回
		return result;
	}

	@Override
	public void changeInfo(User user) throws UserNotFoundException, UpdateException {
		//modified_user应该来源于session
		//根据参数user中的uid查询数据，user.getUid()
		Integer uid=user.getUid();
		User result=userMapper.findByUid(uid);
		//检查查询结果是否存在，是否标记删除
		//判断查询结果是否为null
		//抛出UserNotFoundException异常
		if (result==null) {
			throw new UserNotFoundException("用户不存在");
		}
		//判断is_delete是否为1
		//抛出UserNotFoundException异常
		if (result.getIs_delete()==1) {
			throw new UserNotFoundException("用户已作废");
		}
		//创建时间对象，并将时间对象封装到user中
		Date now=new Date();
		result.setModified_time(now);
		//执行修改mapper.updateInfo(user)
		Integer rows=userMapper.updateInfo(user);
		//判断修改后的记录数是否不为1
		if (rows!=1) {
		//抛出UpdateException异常
			throw new UpdateException("修改个人资料失败，请联系管理员！");
		}

	}


    private String getMd5Password(String password,String salt) {
    	String str=password+salt;
    	for (int i=0;i<=3;i++) {
    		str=DigestUtils.md5Hex(str.getBytes()).toUpperCase();
    	}
    	return str;
    }





}
